package cn.lf.android.lawfirm.conflict;


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.conflict.controller.ConflictListConroller;
import cn.lf.android.lawfirm.conflict.modle.Conflict;
import cn.lf.android.lawfirm.conflict.view.ConflictAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;
import cn.lf.android.lawfirm.worklog.AddWorkLogActivity;
import cn.lf.android.lawfirm.worklog.WorkLogSearchListActivity;

import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.eb_tv_right;
import static cn.lf.android.lawfirm.R.id.img_left;
import static cn.lf.android.lawfirm.R.id.rl_search;

public class ConflictListActivity extends OrdinaryBaseActivity
        implements ConflictListConroller.ConflictListCallback {
    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private ConflictAdapter mConflictAdapter;
    private List<Conflict> list = new ArrayList<Conflict>();

    private ConflictListConroller mConflictListConroller;
    private int pageIndex = 1;

    public static Boolean isRefresh = false;

    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mConflictAdapter = new ConflictAdapter(this, list);
        mListView.setAdapter(mConflictAdapter);
        tvSearch.setVisibility(View.VISIBLE);
        mSearch.setVisibility(View.GONE);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case eb_tv_right:
                Intent intent1 = new Intent(this, AddWorkLogActivity.class);
                startActivity(intent1);
                break;
            case img_left:
                finish();
                break;
            case rl_search:
                Intent intent = new Intent(this, WorkLogSearchListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("利益冲突查询");
        tvSearch.setHint("请输入委托方/对方当事人/涉案当事人");
        mTvRight.setText("添加");
        mTvRight.setOnClickListener(this);
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                list.clear();
                mConflictListConroller.onConflictList("");
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex++;
                mConflictListConroller.onConflictList("");
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(ChangeApprovalListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
        SPUtil spUtil = new SPUtil(context);
        DebugLog.e("Authorization---" + spUtil.getAccessToken());
        mConflictListConroller = new ConflictListConroller(this, this);
        mConflictListConroller.onConflictList("");
    }


    private void getData() {
    }

    @Override
    public void onConflictListSuccessed(List<Conflict> beans) {
        list.addAll(beans);
        mConflictAdapter.notifyDataSetChanged();
    }

    @Override
    public void onConflictListFailed() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }
}

